package cn.lhx.blog.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.dao.DaoArticOrg;
import cn.lhx.blog.formbean.AdminFormBean;
import cn.lhx.blog.formbean.ArticleFormBean;
import cn.lhx.blog.pojo.Article;
import cn.lhx.blog.pojo.ArticleCategory;
import cn.lhx.blog.pojo.ArticleOrigin;
import cn.lhx.blog.service.ArtiCatgService;
import cn.lhx.blog.service.ArticleService;
import cn.lhx.blog.util.BlogInfo;
import cn.lhx.blog.util.PagenationTookit;

/**
 * 执行日志业务操作
 * 
 * @author lhx
 * 
 */
public class ArticleManager extends DispatchAction {
	@Resource
	private ArticleService articleService;
	@Resource
	private ArtiCatgService artiCatgService;

	private static Logger log = Logger
			.getLogger(ArticleManager.class.getName());


	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {
		log.info("派遣方法   method= " + request.getParameter("method"));
		if (null == name) {
			// 默认是查看单篇日志
			return viewArticle(mapping, form, request, response);
		} else {
			return super.dispatchMethod(mapping, form, request, response, name);
		}

	}

	/**
	 * 查看一篇日志
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward viewArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ArticleFormBean atcf = (ArticleFormBean) form;
		// 提取信息
		int id = atcf.getId();
		log.info("查看 单篇日志 ID= " + id);
		Article curArticle = articleService.getArtilceById(id);
		// 填入当前日志
		request.setAttribute("curArticle", curArticle);
		//跳往管理页面
//		return mapping.findForward("viewArticle");
		//跳往前台
		return mapping.findForward("front_viewArticle2");
	}

	/**
	 * 保存日志
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ArticleFormBean atcf = (ArticleFormBean) form;
		// 提取信息
		int id = atcf.getId();

		DaoArticOrg daoArticleOrg = new DaoArticOrg();
		ArticleOrigin origin = daoArticleOrg.getOrgById(atcf.getOrigin());
		String title = atcf.getTitle();
		ArticleCategory catg = artiCatgService.getCategoryById(atcf
				.getCategory());
		String keyWords = atcf.getKeyWords();
		String content = atcf.getContent();
		// 填充信息
		Article article = new Article();
		if (id != 0) {
			article.setId(id);
		}
		article.setOrigin(origin);
		article.setTitle(title);
		article.setCategory(catg);
		article.setKeyWords(keyWords);
		article.setContent(content);
		article.setPostTime(new Date());

		// 保存日志
		articleService.saveArticle(article);
		// 对应分类+1
		artiCatgService.changeArticleCount(catg.getId(), 1);
		log.info("日志被保存了,即将进入后台管理首页");
		//传递消息到消息页面
//		request.setAttribute("message", "保存操作成功！即将进入《后台首页》界面");
//		request.setAttribute("targetURL", "/admin");
		return mapping.findForward("saveOK");
	}

	/**
	 * 读取日志
	 * 
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward loadBlogs(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		log.info("批量读取日志");
		ArticleFormBean aticf = (ArticleFormBean) form;
		// 获取当前页码
		int page = aticf.getPage();
		page = page < 1 ? 1 : page;
		// 获得总共有多少篇日志
		long articleCount = articleService.statsArticleCount();
		// 计算出页数(pages)=articleCount/6
		int pages=PagenationTookit.getPages(BlogInfo.ADMIN_BLOGS_ONE_PAGE, articleCount);
		page = page > pages ? pages : page;
		log.info("page= " + page);
		request.setAttribute("pages", pages);
		request.setAttribute("curPage", page);
		// 设置页码导航条
		int nav[] = PagenationTookit.getPageNavigator(page,
				BlogInfo.ADMIN_CUNT_OF_PAGENUMBER, pages);
		request.setAttribute("nav", nav);
		int base = page - 1;// 基点=第n页 -1
		// 读取几篇日志
		List<Article> articles = articleService.getArticles(base
				* BlogInfo.ADMIN_BLOGS_ONE_PAGE, BlogInfo.ADMIN_BLOGS_ONE_PAGE);
		request.setAttribute("articles", articles);

		return mapping.findForward("showBlogs");
	}

	/**
	 * 删除日志
	 * 
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		ArticleFormBean aticf = (ArticleFormBean) form;
		int _id = aticf.getId();
		// 删除日志
		Article a = articleService.getArtilceById(_id);
		artiCatgService.changeArticleCount(a.getCategory().getId(), -1);
		articleService.delArticle(_id);

		log.info("日志被删除了 Id= " + _id);
		return mapping.findForward("deleteOK");
	}

	/**
	 * 更新日志
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateBlog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		ArticleFormBean atcf = (ArticleFormBean) form;
		// 提取信息
		int id = atcf.getId();

		DaoArticOrg daoArticleOrg = new DaoArticOrg();
		ArticleOrigin origin = daoArticleOrg.getOrgById(atcf.getOrigin());
		String title = atcf.getTitle();
		ArticleCategory catg = artiCatgService.getCategoryById(atcf
				.getCategory());
		String keyWords = atcf.getKeyWords();
		String content = atcf.getContent();
		// 填充信息
		Article article = articleService.getArtilceById(id);
		article.setOrigin(origin);
		article.setTitle(title);
		article.setCategory(catg);
		article.setKeyWords(keyWords);
		article.setContent(content);
		article.setModTime(new Date());

		// 更新日志
		articleService.UpdateArticle(article);
		log.info("日志更新了 ID= " + id);
		//传递消息到消息页面
//		request.setAttribute("message", "更新操作成功！即将进入《全部日志》界面");
//		request.setAttribute("targetURL", "/admin/blogs");
		return mapping.findForward("updateOK");

	}

	/**
	 * 获取写博客所需必要的信息
	 * 
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward writeBlog(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		log.info("获取写博客所需必要的信息，即将进入写日志页面");
		// 开始获取分类信息
		List<ArticleCategory> artCatgList = artiCatgService.getAllCategory();
		request.setAttribute("categories", artCatgList);
		// 来源信息
		DaoArticOrg daoArticleOrg = new DaoArticOrg();
		Iterator<ArticleOrigin> orgIterator = daoArticleOrg.getAllOrg();
		request.setAttribute("origins", orgIterator);

		return mapping.findForward("writeBlog_page");
	}

	/**
	 * 修改日志,获取修改日志所需的信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward modifyBlog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {

		ArticleFormBean atcf = (ArticleFormBean) form;
		int _id = atcf.getId();
		Article curArticle = new Article();
		curArticle = articleService.getArtilceById(_id);
		request.setAttribute("curArticle", curArticle);
		// 开始获取分类信息
		List<ArticleCategory> artCatgList = artiCatgService.getAllCategory();
		request.setAttribute("categories", artCatgList);
		// 来源信息
		DaoArticOrg daoArticleOrg = new DaoArticOrg();
		Iterator<ArticleOrigin> orgIterator = daoArticleOrg.getAllOrg();
		request.setAttribute("origins", orgIterator);

		log.info("去修改日志 ID= " + _id + " ,即将进入编辑界面");

		return mapping.findForward("modifyBlog_page");
	}

}
